package com.ztp.pc.test2;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.ftpserver.DataConnectionConfigurationFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.UserFactory;
import org.apache.log4j.PropertyConfigurator;


public class Main {
	
	public static void main(String [] args){
		
		PropertyConfigurator.configure("log4j.properties");
		
		FtpServerFactory serverFactory = new FtpServerFactory();
		ListenerFactory listenerFactory = new ListenerFactory();
		
		listenerFactory.setPort(2222);
		
		/*
		DataConnectionConfigurationFactory dccf = new DataConnectionConfigurationFactory();
		dccf.setActiveEnabled(true);
		listenerFactory.setDataConnectionConfiguration(dccf.createDataConnectionConfiguration());
		*/
		
		serverFactory.addListener("default", listenerFactory.createListener());
		
		
		PropertiesUserManagerFactory propUsrMngrFctry = new PropertiesUserManagerFactory();
		propUsrMngrFctry.setFile(new File("users.properties"));
		
		
		propUsrMngrFctry.setPasswordEncryptor(new ClearTextPasswordEncryptor());
		final UserManager userManager = propUsrMngrFctry.createUserManager();
		
		/*	UserFactory userFactory = new UserFactory();
			userFactory.setName("awtowaz");
			userFactory.setPassword("awtowaz");
			userFactory.setHomeDirectory("e:/");
		try {
			userManager.save(userFactory.createUser());
		} catch (FtpException e1) {
			e1.printStackTrace();
			System.exit(666);
		}
		*/
		serverFactory.setUserManager(userManager);
		//serverFactory.setUserManager(propUsrMngrFctry.createUserManager());
		
		final FtpServer server = serverFactory.createServer();
		
		try {
			server.start();
		} catch (FtpException e) {
			e.printStackTrace();
		}
		
		Runnable getchar = new Runnable() {
			@Override
			public void run() {
				char [] buffer = new char[8];
				InputStreamReader isr = new InputStreamReader(System.in);
				
				try {
					while(true){
						Thread.sleep(2500);
						if(isr.ready()){
							isr.read(buffer, 0, 8);
							if(buffer[0] == 'k' && (buffer[1] == '\n' || buffer[1] == '\r' )){
								break;
							}
							else if(buffer[0] == 'a' && (buffer[1] == '\n' || buffer[1] == '\r' )){
								UserFactory userFactory = new UserFactory();
								userFactory.setName("awtowaz");
								userFactory.setPassword("awtowaz");
								userFactory.setHomeDirectory("e:/ftpserver");
								try {
									userManager.save(userFactory.createUser());
								} catch (FtpException e) {
									e.printStackTrace();
								}
							}	
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					System.out.println("input reader has been murdered 0:0:0:");
					e.printStackTrace();
				}
				finally{
					server.stop();
					System.out.println("trying to close input stream...");
					try {
						isr.close();
						System.out.println("...input stream closed");
					} catch (IOException e1) {
						System.out.println("...cannot close");
						e1.printStackTrace();
					}
				}
			}
		};
		Thread tgetchar = new Thread(getchar);
		tgetchar.start();
		
	}
}
